package cn.acewill.yxd.yunxundian.utils;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

/**
 * 自定义popupWindow
 */
public class SpinnerPopWin extends PopupWindow {
	private View conentView;
	private Activity context;
	private static SpinnerPopWin instance = null;

	/**
	 * width 属性值 LayoutParams.MATCH_PARENT等
	 * 
	 * @auther Sanmel
	 * @date 2016年5月26日
	 */
	public static SpinnerPopWin getInstance(Activity context, View contentView) {
		if (instance == null
				|| (instance != null && instance.context != context)) {
			instance = new SpinnerPopWin(context);
		}
		instance.conentView = contentView;
		instance.init();
		return instance;
	}

	private SpinnerPopWin(Activity context) {
		this.context = context;
	}

	public void init() {
		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置默认高度
		this.setHeight(context.getWindowManager().getDefaultDisplay()
				.getHeight() / 5 * 2);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
	}

	/**
	 * 显示popupWindow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, 0,0);
		} else {
			this.dismiss();
		}
	}
}
